from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from .database import engine
from .models import Base
from .routers import auth, users, roles, files, notices, mp, courses

# 创建数据库表
Base.metadata.create_all(bind=engine)

app = FastAPI(title="后台管理系统API", version="1.0.0")

# 配置CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # 在生产环境中应该设置具体的前端地址
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 注册路由
app.include_router(auth.router)
app.include_router(users.router)
app.include_router(roles.router)
app.include_router(files.router)
app.include_router(notices.router)
app.include_router(mp.router)
app.include_router(courses.router)

# 根路径
@app.get("/")
async def root():
    return {"message": "后台管理系统API"}

# 健康检查
@app.get("/health")
async def health_check():
    return {"status": "healthy"}